package com.service.sys;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson.JSON;
import com.dao.sys.SysManageMapper;
import com.dao.sys.SysModuleMapper;
import com.dao.sys.SysModulePermissionMapper;
import com.dao.sys.SysRoleMapper;
import com.dao.sys.SysRoleModuleMapper;
import com.dao.sys.SysRoleModulePermissionMapper;
import com.dao.sys.SysUserMapper;
import com.dao.sys.SysUserRoleMapper;
import com.model.sys.SysModule;
import com.model.sys.SysModulePermission;
import com.model.sys.SysRole;
import com.model.sys.SysRoleModule;
import com.model.sys.SysRoleModulePermission;
import com.model.sys.SysUser;
import com.model.sys.SysUserRole;

@Service
public class SysService {
	static final Logger logger = LoggerFactory.getLogger(SysService.class);
	@Autowired
	private SysUserMapper sysUserMapper;
	@Autowired
	private SysUserRoleMapper sysUserRoleMapper;
	@Autowired
	private SysModuleMapper sysModuleMapper;
	@Autowired
	private SysModulePermissionMapper sysModulePermissionMapper;
	@Autowired
	private SysRoleModuleMapper sysRoleModuleMapper;
	@Autowired
	private SysRoleModulePermissionMapper sysRoleModulePermissionMapper;
	@Autowired
	private SysRoleMapper sysRoleMapper;
	@Autowired
	private SysManageMapper sysManageMapper;

	public String getDeptChild(String deptid) {
		List<HashMap<String, Object>> list = sysManageMapper.selectDeptChildMap(deptid);
		if (list == null || list.size() == 0) {
			list = sysManageMapper.selectDeptUsersMap(deptid);
		}
		String json = JSON.toJSONString(list);
		logger.info("getDeptChild:{}", json);
		return json;
	}

	@Transactional
	public int addUser(SysUser sysUser, String roleString) {
		int i = 0;
		i = sysUserMapper.insert(sysUser);
		String[] roleList = roleString.split(",");
		for (String role : roleList) {
			SysUserRole sysUserRole = new SysUserRole();
			sysUserRole.setUserid(sysUser.getUserid());
			sysUserRole.setRoleid(role);
			sysUserRoleMapper.insert(sysUserRole);
		}
		return i;
	}

	@Transactional
	public int updateUser(SysUser sysUser, String roleString) {
		int i = 0;
		i = sysUserMapper.updateByPrimaryKey(sysUser);
		String[] roleList = roleString.split(",");
		sysUserRoleMapper.deleteByUserid(sysUser.getUserid());
		for (String role : roleList) {
			SysUserRole sysUserRole = new SysUserRole();
			sysUserRole.setUserid(sysUser.getUserid());
			sysUserRole.setRoleid(role);
			sysUserRoleMapper.insert(sysUserRole);
		}
		return i;
	}

	@Transactional
	public int deleteUser(String userid) {
		int i = 0;
		i = sysUserMapper.deleteByPrimaryKey(userid);
		sysUserRoleMapper.deleteByUserid(userid);
		return i;
	}

	@Transactional(rollbackFor = Exception.class)
	public int addModule(SysModule modelModule) {
		int i = 0;
		i = sysModuleMapper.insert(modelModule);
		String[] liStrings = modelModule.getActionstring().split(",");
		for (String s : liStrings) {
			if (s == null || s.equals("")) {
				continue;
			}
			SysModulePermission record = new SysModulePermission();
			record.setModuleid(modelModule.getModuleid());
			record.setPermission(s);
			sysModulePermissionMapper.insert(record);
		}
		return i;
	}

	@Transactional(rollbackFor = Exception.class)
	public int updateModule(SysModule modelModule) {
		int i = 0;
		i = sysModuleMapper.updateByPrimaryKey(modelModule);
		sysModulePermissionMapper.deleteById(modelModule.getModuleid());
		String[] liStrings = modelModule.getActionstring().split(",");
		String permiString = "";
		permiString += "(";
		for (String s : liStrings) {
			if (s == null || s.equals("")) {
				continue;
			}
			SysModulePermission record = new SysModulePermission();
			record.setModuleid(modelModule.getModuleid());
			record.setPermission(s);
			sysModulePermissionMapper.insert(record);
			permiString += "'" + s + "'";
			if (!s.equals(liStrings[liStrings.length - 1])) {
				permiString += ",";
			}
		}
		permiString += ")";
		sysRoleModulePermissionMapper.deleteByIdPermission(modelModule.getModuleid(), permiString);
		return i;
	}

	@Transactional
	public int deleteModule(String moduleidString) {
		int i = 0;
		i = sysModuleMapper.deleteByPrimaryKey(moduleidString);
		sysModulePermissionMapper.deleteById(moduleidString);
		sysRoleModuleMapper.deleteByModuleid(moduleidString);
		sysRoleModulePermissionMapper.deleteByModuleid(moduleidString);
		return i;
	}

	@Transactional
	public int addRole(SysRole modelModule, String permiString, Map<String, String> map) {
		int i = 0;
		i = sysRoleMapper.insert(modelModule);
		if (permiString != null && !permiString.equals("")) {
			String[] liStrings = permiString.split(",");
			for (String s : liStrings) {
				SysRoleModulePermission sysRoleModulePermission = new SysRoleModulePermission();
				sysRoleModulePermission.setRoleid(modelModule.getRoleid());
				String s0 = s.split("\\$")[0];
				String s1 = s.split("\\$")[1];
				sysRoleModulePermission.setModuleid(s0);
				sysRoleModulePermission.setPermission(s1);
				sysRoleModulePermissionMapper.insert(sysRoleModulePermission);
			}
			String modelidString = "";
			for (String s : liStrings) {
				String moduleidString = s.split("\\$")[0] + ",";
				if (modelidString.contains(modelidString)) {
					continue;
				}
				modelidString += moduleidString;
				SysRoleModule model = new SysRoleModule();
				model.setModuleid(moduleidString);
				model.setRoleid(modelModule.getRoleid());
				model.setDbPermission(map.get(model.getModuleid()));
				sysRoleModuleMapper.insert(model);
			}
		}
		return i;
	}

	@Transactional
	public int updateRole(SysRole modelModule, String permiString, Map<String, String> map) {
		int i = 0;
		i = sysRoleMapper.updateByPrimaryKey(modelModule);
		sysRoleModuleMapper.deleteByRoleid(modelModule.getRoleid());
		sysRoleModulePermissionMapper.deleteByRoleid(modelModule.getRoleid());
		if (permiString != null && !permiString.equals("")) {
			String[] liStrings = permiString.split(",");
			for (String s : liStrings) {
				SysRoleModulePermission sysRoleModulePermission = new SysRoleModulePermission();
				sysRoleModulePermission.setRoleid(modelModule.getRoleid());
				sysRoleModulePermission.setModuleid(s.split("\\$")[0]);
				sysRoleModulePermission.setPermission(s.split("\\$")[1]);
				sysRoleModulePermissionMapper.insert(sysRoleModulePermission);
			}
			String modelidString = "";
			for (String s : liStrings) {
				String sssString = s.split("\\$")[0];
				if (s.split("\\$")[0].equals("1")) {
					String string = "";
					System.out.println("aaaaa");
				}
				String moduleidString = s.split("\\$")[0] + ",";
				if (modelidString.contains(moduleidString)) {
					continue;
				}
				modelidString += moduleidString;
				SysRoleModule model = new SysRoleModule();
				model.setModuleid(s.split("\\$")[0]);
				model.setRoleid(modelModule.getRoleid());
				model.setDbPermission(map.get(model.getModuleid()));
				sysRoleModuleMapper.insert(model);
			}
		}
		return i;
	}

	@Transactional
	public int deleteRole(String roleidString) {
		int i = 0;
		i = sysRoleMapper.deleteByPrimaryKey(roleidString);
		sysRoleModuleMapper.deleteByRoleid(roleidString);
		sysRoleModulePermissionMapper.deleteByRoleid(roleidString);
		sysUserRoleMapper.deleteByRoleid(roleidString);
		return i;
	}
}
